package com.runyu.cordova.plugin;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import javax.crypto.Cipher;

public class EncryptionHelper {
    public static String encryptData(String data, PublicKey publicKey) throws Exception {
        // 在实际应用中使用更安全的加密方式
        byte[] encryptedBytes = data.getBytes();
        byte[] encryptedData = encryptUsingPublicKey(encryptedBytes, publicKey);
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    private static byte[] encryptUsingPublicKey(byte[] data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    }


    public static String decryptData(String encryptedData, PrivateKey privateKey) throws Exception {
        byte[] decodedData = new byte[0];
        decodedData = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedData = decryptUsingPrivateKey(decodedData, privateKey);
        return new String(decryptedData);
    }
    private static byte[] decryptUsingPrivateKey(byte[] data, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(data);
    }



    public static PublicKey getPublicKeyFromBytes(byte[] keyBytes) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA", "SC");
        return keyFactory.generatePublic(new X509EncodedKeySpec(keyBytes));
    }


    public static String encryptDataByPrivateKey(String data, PrivateKey privateKey) throws Exception {
        // 在实际应用中使用更安全的加密方式
        byte[] encryptedBytes = data.getBytes();
        byte[] encryptedData = encryptUsingPrivateKey(encryptedBytes, privateKey);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            return Base64.getEncoder().encodeToString(encryptedData);
//        }else{
//            return new String(encryptedData, StandardCharsets.UTF_8);
//        }
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    private static byte[] encryptUsingPrivateKey(byte[] data, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return cipher.doFinal(data);
    }


    public static String decryptDataByPublicKey(String encryptedData, PublicKey publicKey) throws Exception {
        byte[] decodedData = new byte[0];
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//            decodedData = Base64.getDecoder().decode(encryptedData);
//        }else{
//            decodedData = encryptedData.getBytes(StandardCharsets.UTF_8);
//        }
        decodedData = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedData = decryptUsingPublicKey(decodedData, publicKey);
        return new String(decryptedData);
    }
    private static byte[] decryptUsingPublicKey(byte[] data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    }
}

